/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClientsAndHotels.gui;

import ClientsAndHotels.types.HotelParams;
import ClientsAndHotels.types.Room;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

/**
 * 
 * @author lgi
 */
public class HotelFrame extends javax.swing.JDialog {

    private AddRoomFrame addRoomFrame;
    private ArrayList<Room> rooms;
    private boolean cancelled;

    public ArrayList<Room> getRooms() {
        return rooms;
    }

    public boolean isCancelled() {
        return cancelled;
    }

    /**
     * Creates new form HotelFrame
     */
    public HotelFrame() {
        initComponents();
        addRoomFrame = new AddRoomFrame();
        rooms = new ArrayList<>();
        addRoomFrame.setModal(true);
        DefaultTableModel model = new DefaultTableModel(new String[] { "Numer",
                "Ilość miejsc", "Cena" }, 0);
        roomsTable.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cityField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        starsField = new javax.swing.JSpinner();
        jScrollPane1 = new javax.swing.JScrollPane();
        roomsTable = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        addButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        addRoomButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Dodaj hotel");

        jLabel1.setText("Nazwa:");

        jLabel2.setText("Miasto:");

        jLabel3.setText("Gwiazdki:");

        starsField.setModel(new javax.swing.SpinnerNumberModel(3, 1, 5, 1));

        roomsTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, null, null },
                        { null, null, null, null }, { null, null, null, null },
                        { null, null, null, null } }, new String[] { "Title 1",
                        "Title 2", "Title 3", "Title 4" }));
        jScrollPane1.setViewportView(roomsTable);

        jLabel4.setText("Pokoje:");

        addButton.setText("Dodaj");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onAddButtonClick(evt);
            }
        });

        cancelButton.setText("Anuluj");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onCancelButtonClick(evt);
            }
        });

        addRoomButton.setText("Dodaj pokój");
        addRoomButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onAddRoomButtonClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(
                                                                                        jLabel3)
                                                                                .addComponent(
                                                                                        jLabel2)
                                                                                .addComponent(
                                                                                        jLabel1))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(
                                                                                        nameField)
                                                                                .addComponent(
                                                                                        cityField)
                                                                                .addComponent(
                                                                                        starsField)))
                                                .addComponent(jLabel4)
                                                .addComponent(
                                                        jScrollPane1,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        0, Short.MAX_VALUE)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addComponent(
                                                                        addButton)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addComponent(
                                                                        cancelButton))
                                                .addGroup(
                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                        layout.createSequentialGroup()
                                                                .addGap(0,
                                                                        187,
                                                                        Short.MAX_VALUE)
                                                                .addComponent(
                                                                        addRoomButton)))
                                .addContainerGap()));
        layout.setVerticalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel1)
                                                .addComponent(
                                                        nameField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel2)
                                                .addComponent(
                                                        cityField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel3)
                                                .addComponent(
                                                        starsField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        134, Short.MAX_VALUE)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(addRoomButton)
                                .addGap(15, 15, 15)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(addButton)
                                                .addComponent(cancelButton))
                                .addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onCancelButtonClick(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_onCancelButtonClick
        cancelled = true;
        setVisible(false);
    }// GEN-LAST:event_onCancelButtonClick

    private void onAddButtonClick(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_onAddButtonClick
        cancelled = false;
        setVisible(false);
    }// GEN-LAST:event_onAddButtonClick

    private void onAddRoomButtonClicked(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_onAddRoomButtonClicked
        addRoomFrame.setVisible(true);
        Room room = addRoomFrame.getRoom();
        if (room != null) {
            rooms.add(room);
            ((DefaultTableModel) roomsTable.getModel()).addRow(new Object[] {
                    room.getRoomId(), room.getPlaceFor(), room.getPrice() });
        }
    }// GEN-LAST:event_onAddRoomButtonClicked

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        // <editor-fold defaultstate="collapsed"
        // desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase
         * /tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
                    .getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException 
                | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HotelFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        }
        // </editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new HotelFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton addRoomButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField cityField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nameField;
    private javax.swing.JTable roomsTable;
    private javax.swing.JSpinner starsField;

    // End of variables declaration//GEN-END:variables

    String getHotelName() {
        return nameField.getText();
    }

    int getHotelRooms() {
        if (rooms != null) {
            return rooms.size();
        }
        return 0;
    }

    int getHotelStars() {
        return (Integer) starsField.getValue();
    }

    public String getHotelCity() {
        return cityField.getText();
    }

    public HotelParams getHotel() {
        return new HotelParams(getHotelName(), getHotelCity(), getHotelStars(),
                getRooms());
    }    
    
    public void reset() {
        cancelled = true;
        nameField.setText("");
        cityField.setText("");
        starsField.setValue(new Integer(3));
        while (((DefaultTableModel) roomsTable.getModel()).getRowCount() > 0) {
            ((DefaultTableModel) roomsTable.getModel()).removeRow(0);
        }
    }
}
